package cn.wwxula.common.exception;

import cn.wwxula.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static cn.wwxula.common.constant.ResultCodeConstant.SERVER_ERROR;

/**
 * @author liyang23
 * @date 2024/12/09 17:56
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 未知异常
     * @return 表现层数据封装
     */
    @ExceptionHandler(Exception.class)
    public Result<Object> handleException(Exception e) {
        e.printStackTrace();
        log.error("An unexpected exception occurred...");
        return Result.fail(SERVER_ERROR, "系统开小差了，请稍后再试...");
    }

    @ExceptionHandler(CommonException.class)
    public Result<Object> handleCommonException(CommonException e) {
        log.error(e.getMessage());
        return Result.fail(e.getCode(), e.getMessage());
    }
}
